import { login } from 'api/login'
import Cookies from 'js-cookie'
import md5 from 'md5'
import { resolve } from 'path'

const user = {
  state: {
    account: Cookies.get('account'),
    password: Cookies.get('password')
  },
  mutations: {
    SET_STATE: (state, account) => {
      state.account = account
    },
    SET_PASSWORD: (state, password) => {
      state.password = password
    }
  },
  actions: {
    LoginByUsername ({commit}, userInfo) {
      const account = userInfo.account.trim()
      const password = md5(userInfo.password.trim())
      const rUname = userInfo.rUname
      const rPass = userInfo.rPass
      return new Promise((resolve, reject) => {
        login(account, password).then(res => {
          if (rUname) {
            Cookies.set('account', account)
            commit('SET_STATE', account)
          }
          if (rPass) {
            Cookies.set('password', userInfo.password.trim())
            commit('SET_PASSWORD', userInfo.password.trim())
          }
        })
      })
    }
  }
}
export default user
